/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package axil.framework.localization;

import axil.api.Context;
import axil.api.Locale;
import axil.api.Persona;
import axil.framework.localization.xml.ResourceElement;
import axil.stdlib.text.type.Str;

import java.io.IOException;
import java.io.Writer;

import static axil.framework.Functions.persona;


/**
 * A translation of a key. The translation will hold all of the values for the
 * locales that are actually used.
 */
public class Translation {
	private static final int size = Locality.all().size();

	private final String key;
	private final TranslationValue[] values;


	public Translation(String key) {
		this.key = key;
		this.values = new TranslationValue[size];
	}


	private TranslationValue find(Locale loc) {
		Locality locale = (Locality)loc;
		final int i = locale.index();
		TranslationValue v = values[i];
		if (v == null) {
			Locality fallback = locale.fallback();
			if (fallback == null) {
				v = new Str("(? " + key() + " ?)");
			} else {
				v = find(fallback);
			}
			values[i] = v;
		}
		return v;
	}


	protected String key() {
		return key;
	}


	/**
	 * Apply this given translation value definition to this object. The given
	 * element and locale are never null.
	 */
	protected void apply(Locality locale, ResourceElement element) {
		values[locale.index()] = element.construct();
	}


	public String text() {
		return text(persona(), 1);
	}


	/**
	 * Return the text value appropriate for the given locale.
	 */
	public String text(Persona persona) {
		return text(persona, 1);
	}


	/**
	 * Return the text value appropriate for the given locale and size. The
	 * size is used to select an appropriate translation when plurals are
	 * involved.
	 */
	public String text(Persona persona, int size) {
		return find(persona.locale()).text(size);
	}


	/**
	 * Return the translated, localized and formatted contents of this
	 * translation.
	 */
	public String format(Persona persona, Context values) {
		return format(persona, 1, values);
	}


	/**
	 * Return the translated, localized and formatted contents of this
	 * translation. The size is used to select an appropriate translation when
	 * plurals are involved.
	 */
	public String format(Persona persona, int size, Context values) {
		return find(persona.locale()).format(persona, size, values);
	}


	/**
	 * Write the translated, localized and formatted contents of this
	 * translation into the given output stream. The size is used to select an
	 * appropriate translation when plurals are involved.
	 */
	public void write(Persona persona, int size,
	                  Writer output, Context values) throws IOException {
		find(persona.locale()).write(persona, size, output, values);
	}


	/**
	 * Return a string representation of this object. The string returned is
	 * never null.
	 */
	public String toString() {
		return text();
	}
}
